package com.gao.sentinel.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RedisPropertiesConfig {

    /** 主节点 */
    @Autowired
    private RedisMaster redisMaster;

    /** 从节点 */
    @Autowired
    private RedisSlave redisSlaves;

    /** 哨兵 */
    @Autowired
    private RedisSentinel redisSentinel;

    public RedisMaster getRedisMaster() {
        return redisMaster;
    }

    public void setRedisMaster(RedisMaster redisMaster) {
        this.redisMaster = redisMaster;
    }

    public RedisSlave getRedisSlaves() {
        return redisSlaves;
    }

    public void setRedisSlaves(RedisSlave redisSlaves) {
        this.redisSlaves = redisSlaves;
    }

    public RedisSentinel getRedisSentinel() {
        return redisSentinel;
    }

    public void setRedisSentinel(RedisSentinel redisSentinel) {
        this.redisSentinel = redisSentinel;
    }


}

/**
 * master
 */
@Configuration
@ConfigurationProperties(prefix = "spring.redis.master")
class RedisMaster {

    private MyRedisNode redisNode;

    private RedisSentinel sentinel;

    public MyRedisNode getRedisNode() {
        return redisNode;
    }

    public void setRedisNode(MyRedisNode redisNode) {
        this.redisNode = redisNode;
    }

    public RedisSentinel getSentinel() {
        return sentinel;
    }

    public void setSentinel(RedisSentinel sentinel) {
        this.sentinel = sentinel;
    }
}

/**
 * slave
 */
@Configuration
@ConfigurationProperties(value = "spring.redis.slave")
class RedisSlave {

    private List<MyRedisNode> redisNode;

    public List<MyRedisNode> getRedisNode() {
        return redisNode;
    }

    public void setRedisNode(List<MyRedisNode> redisNode) {
        this.redisNode = redisNode;
    }

}

/**
 * sentinel redis
 */
@Configuration
@ConfigurationProperties("spring.redis.master.sentinel")
class RedisSentinel {

    private String master;

    @Value("#{'${spring.redis.master.redisNode.sentinel.node}'.split(',')}")
    private List<String> node;

    private String password;

    public String getMaster() {
        return master;
    }

    public void setMaster(String master) {
        this.master = master;
    }

    public List<String> getNode() {
        return node;
    }

    public void setNode(List<String> node) {
        this.node = node;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}


class MyRedisNode {

    private String host;

    private int port;

    private String username;

    private String password;

    private int database;

    private Long timeout;

    private RedisSentinel sentinel;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getDatabase() {
        return database;
    }

    public void setDatabase(int database) {
        this.database = database;
    }

    public Long getTimeout() {
        return timeout;
    }

    public void setTimeout(Long timeout) {
        this.timeout = timeout;
    }

    public RedisSentinel getSentinel() {
        return sentinel;
    }

    public void setSentinel(RedisSentinel sentinel) {
        this.sentinel = sentinel;
    }
}